package com.by.and_widget.banner;

import android.content.Context;
import android.database.DataSetObserver;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ViewFlipper;

/**
 * 广告Banner控件
 */
public class AutoScrollerViewFilpper extends ViewFlipper {
    private static int INTERVAL_TIME = 4000;

    private boolean isAutoScroll = true;
    private int current = 0;
    private OnPageSelectListener mOnPageSelectListener;
    private BaseAdapter mAdapter;
    private AdapterView.OnItemClickListener mOnItemClickListener;

    public AutoScrollerViewFilpper(Context context) {
        super(context);
        init();
    }

    public AutoScrollerViewFilpper(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setDelayTime(INTERVAL_TIME);
    }


    public void setDelayTime(int time) {
        INTERVAL_TIME = time;
        setFlipInterval(INTERVAL_TIME);
    }

    public void setOnPageSelectListener(OnPageSelectListener l) {
        mOnPageSelectListener = l;
    }

    public void setIsAutoScroll(boolean isScroll) {
        isAutoScroll = isScroll;
        if (!isAutoScroll) {
            stopFlipping();
        }
    }

    @Override
    public void showNext() {
        super.showNext();
        current++;
        if (mOnPageSelectListener != null && getCount() > 0) {
            mOnPageSelectListener.onPageSelect(current % mAdapter.getCount());
        }
        if (isAutoScroll && !isFlipping()) {
            postDelayed(new Runnable() {
                @Override
                public void run() {
                    startFlipping();
                }
            }, INTERVAL_TIME);
        }
    }

    @Override
    public void showPrevious() {
        super.showPrevious();
        if (current <= 0) {
            current = getCount();
        } else {
            current--;
        }
        if (getCount() > 0 && mOnPageSelectListener != null) {
            mOnPageSelectListener.onPageSelect(current % mAdapter.getCount());
        }
        if (isAutoScroll && !isFlipping()) {
            postDelayed(new Runnable() {
                @Override
                public void run() {
                    startFlipping();
                }
            }, INTERVAL_TIME);
        }
    }

    private int getCount() {
        return getAdapter() == null ? 0 : getAdapter().getCount();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (isAutoScroll) {
            startFlipping();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (isAutoScroll) {
            stopFlipping();
        }
    }

    public void setAdapter(BaseAdapter adapter) {
        if (mAdapter != null) {
            mAdapter.unregisterDataSetObserver(mObserver);
        }
        removeAllViews();
        mAdapter = adapter;
        if (mAdapter != null) {
            if (getCount() > 0) {
                startFlipping();
            }
            mAdapter.registerDataSetObserver(mObserver);
            mAdapter.notifyDataSetInvalidated();
        }
    }

    public BaseAdapter getAdapter() {
        return mAdapter;
    }

    public void bindLinearLayout() {
        removeAllViews();
        int count = mAdapter.getCount();
        for (int i = 0; i < count; i++) {
            View v = mAdapter.getView(i, null, null);
            final int position = i;
            if (mOnItemClickListener != null) {
                v.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        mOnItemClickListener.onItemClick(null, v, position, 0L);
                    }
                });
            }
            addView(v, i);
        }
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    private DataSetObserver mObserver = new DataSetObserver() {
        @Override
        public void onChanged() {
            bindLinearLayout();
        }

        @Override
        public void onInvalidated() {
            bindLinearLayout();
        }
    };

    public interface OnPageSelectListener {
        void onPageSelect(int pos);
    }
}
